import {
  queryListData,
  addData,
  updateDataInfo,
  updateDataDefault,
  updateDataStatus,
  removeData,
} from '../services/warehouse';

export default {
  namespace: 'warehouse',

  state: {
    warehouse: [],
  },

  effects: {
    *list({ payload }, { call, put }) {
      const response = yield call(queryListData, payload);
      yield put({
        type: 'handleListData',
        payload: response,
      });
    },
    *add({ payload }, { call, put }) {
      const response = yield call(addData, payload);
      yield put({
        type: 'handleAddData',
        payload: response,
      });
    },
    *updateInfo({ payload }, { call, put }) {
      const response = yield call(updateDataInfo, payload);
      yield put({
        type: 'handleUpdateData',
        payload: response,
      });
    },
    *updateDefault({ payload }, { call, put }) {
      const response = yield call(updateDataDefault, payload);
      yield put({
        type: 'handleUpdateDefaultData',
        payload: response,
      });
    },
    *updateStatus({ payload }, { call, put }) {
      const response = yield call(updateDataStatus, payload);
      yield put({
        type: 'handleUpdateStatusData',
        payload: response,
      });
    },
    *remove({ payload }, { call, put }) {
      const response = yield call(removeData, payload);
      yield put({
        type: 'handleRemoveData',
        payload: response,
      });
    },
  },

  reducers: {
    handleListData(state, action) {
      let d = action.payload;

      if (d === undefined) {
        d = {
          list: [],
        };
      }

      const { status } = d;

      if (status === 200) {
        d.pagination = {
          total: d.total,
          pageSize: d.pageSize,
          current: parseInt(d.pageNo, 10) || 1,
        };
      } else {
        d.pagination = {
          total: 0,
          pageSize: 10,
          current: 1,
        };
      }
      return {
        ...state,
        data: d,
      };
    },
    handleAddData(state, action) {
      let d = action.payload;

      if (d === undefined) {
        d = {
          state: -10001,
          message: '接口调用错误',
        };
      }

      return {
        ...state,
        data: d,
      };
    },
    handleUpdateData(state, action) {
      let d = action.payload;

      if (d === undefined) {
        d = {
          state: -10001,
          message: '接口调用错误',
        };
      }

      return {
        ...state,
        data: d,
      };
    },
    handleUpdateDefaultData(state, action) {
      let d = action.payload;

      if (d === undefined) {
        d = {
          state: -10001,
          message: '接口调用错误',
        };
      }
      return {
        ...state,
        data: d,
      };
    },
    handleUpdateStatusData(state, action) {
      let d = action.payload;

      if (d === undefined) {
        d = {
          state: -10001,
          message: '接口调用错误',
        };
      }

      return {
        ...state,
        data: d,
      };
    },
    handleRemoveData(state, action) {
      let d = action.payload;

      if (d === undefined) {
        d = {
          state: -10001,
          message: '接口调用错误',
        };
      }

      return {
        ...state,
        data: d,
      };
    },
  },
};
